package org.ljy.lotterysystem.dao.mapper;

import org.apache.ibatis.annotations.*;
import org.ljy.lotterysystem.dao.dataobject.Encrypt;
import org.ljy.lotterysystem.dao.dataobject.UserDO;

import java.util.List;

//编写用户相关的sql

@Mapper
public interface UserMapper {

    /**
     * 查询用户绑定的邮箱号
     * @param email
     * @return
     */
    @Select("select count(*) from user where email = #{email}")
    int countByEmail(@Param("email") String email);

    @Select("select count(*) from user where phone_number = #{phoneNumber}")
    int countByPhone(@Param("phoneNumber") Encrypt phoneNumber);


    @Insert("insert into user (user_name, email, phone_number, password, identity) " +
            "values (#{userName},#{email},#{phoneNumber},#{password},#{identity})")
    @Options(useGeneratedKeys = true,keyProperty = "id",keyColumn = "id")
    void insert(UserDO user);


    @Select("select * from user where phone_number = #{phoneNumber}")
    UserDO selectByPhoneNumber(@Param("phoneNumber") Encrypt phoneNumber);

    @Select("select * from user where email = #{mail}")
    UserDO selectByMail(@Param("mail") String mail);

    /**
     *  根据人员身份返回人员列表
     * @param identity 为null时，查询所有人员列表
     * @return
     */

    @Select("<script>" +
            " select * from user" +
            " <if test= \"identity!=null\" >" +
            "    where identity = #{identity}" +
            " </if>" +
            " order by id desc" +
            " </script>")
    List<UserDO> selectUserListByIdentity(@Param("identity") String identity);


    /**
     *  检查id集是否存在
     * @param
     * @return 存在于数据库的id集
     */
    @Select("<script>" +
            " select id from user" +
            " where id in" +
            " <foreach item='item' collection='items' open='(' separator=',' close=')'>" +
            " #{item}" +
            " </foreach>" +
            " </script>")
    List<Long> selectExistByIds(@Param("items") List<Long> ids);


    @Select("<script>" +
            " select * from user" +
            " where id in" +
            " <foreach item='item' collection='items' open='(' separator=',' close=')'>" +
            " #{item}" +
            " </foreach>" +
            " </script>")
    List<UserDO> batchSelectByIds(@Param("items")List<Long> ids);
}
